package map.tile.server.service.impl;

import map.tile.server.bo.MongodbTile;
import map.tile.server.bo.Tile;
import map.tile.server.dao.BaseTileDao;
import map.tile.server.dto.BoundingBox;
import map.tile.server.dto.TileIndex;
import map.tile.server.service.BaseTileService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.stereotype.Service;

import java.util.List;

import static map.tile.server.common.Translate.GetTMSCoord;
import static map.tile.server.common.Translate.GetTMSCoordMector;


@Service
public class BaseTileServiceImpl implements BaseTileService {

    @Autowired
    BaseTileDao baseTileDao;
    @Autowired
    MongodbServiceImpl mongodbService;

    @Override
    public byte[] getTile(TileIndex tileIndex) throws Exception {
        if(StringUtils.isBlank(tileIndex.getCollectionName())){
            throw new Exception("CollectionName不能为空");
        }
        MongodbTile mongodbTile1=mongodbService.getTile(tileIndex);
        if(mongodbTile1!=null){
            return mongodbTile1.getTile();

        }else {
            throw new Exception("无切片");
        }
//        //Todo 判断表名是否存在
//        Tile tile= baseTileDao.getTileByIndex(tileIndex);
//        if(null==tile){
//            throw new Exception("当前范围无切片");
//        }
//        mongodbService.createCollection(tileIndex.getCollectionName());
//        MongodbTile mongodbTile=new MongodbTile();
//        mongodbTile.setX(tileIndex.getX());
//        mongodbTile.setY(tileIndex.getY());
//        mongodbTile.setZ(tileIndex.getZ());
//        mongodbTile.setTile(tile.getTile());
//        mongodbService.saveTile(mongodbTile,tileIndex.getCollectionName());

//        return tile.getTile();
    }

    /**
     * 整表切片
     *
     * @param tableName
     * @param minZoom
     * @param maxZoom
     */
    @Override
    public void TileTable(String tableName, Integer minZoom, Integer maxZoom) {
        mongodbService.createCollection(tableName);

        BoundingBox boundingBox= baseTileDao.getBoundingBoxOfTable(tableName);
        for (int i = minZoom; i <=maxZoom ; i++) {
            List<TileIndex> tileIndexList=GetTMSCoordMector(boundingBox,i,0);
            for (int j = 0; j < tileIndexList.size(); j++) {
                TileIndex tileIndex=tileIndexList.get(j);
                tileIndex.setCollectionName(tableName);
                MongodbTile mongodbTile=new MongodbTile();
                mongodbTile.setX(tileIndex.getX());
                mongodbTile.setY(tileIndex.getY());
                mongodbTile.setZ(tileIndex.getZ());
                Tile tile= baseTileDao.getTileByIndex(tileIndex);
                mongodbTile.setTile(tile.getTile());
                mongodbService.saveTile(mongodbTile,tileIndex.getCollectionName());
            }
        }



    }

//    public List<TileIndex> getTileIndexByBound(Integer zoom,BoundingBox boundingBox){
//        double latMin= boundingBox.getLatMin();
//        double latMax= boundingBox.getLatMax();
//        double lonMin= boundingBox.getLonMin();
//        double lonMax= boundingBox.getLonMax();
//
//    }
}
